package com.bw.common.network.inteceptor;

import com.blankj.utilcode.util.SPUtils;

import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
 * Token拦截器，在okhttp发起请求且接口中包含token时使用
 * */
public class TonkenInteceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        //获取原始接口请求得request->请求地址，请求方式，请求头，请求体
        Request oldRequest = chain.request();
        //新建request->添加token使用
        Request.Builder builder = new Request.Builder();
        builder.url(oldRequest.url());
        builder.headers(oldRequest.headers());
        builder.addHeader("token", SPUtils.getInstance().getString("token"));
        builder.post(oldRequest.body());
        return chain.proceed(builder.build());
    }
}
